package concurrent;


/**
 * 两个线程轮询打印
 * */
public class NotifyThreadPrint implements Runnable {

    private String[] arr;
    private Object lock;

    public NotifyThreadPrint(String[] arr, Object lock) {
        this.arr = arr;
        this.lock = lock;
    }

    public static void main(String[] args) {

        Object lock = new Object();

        Thread t1 = new Thread(new NotifyThreadPrint(new String[]{"1", "2", "3", "4", "5", "6"}, lock));
        Thread t2 = new Thread(new NotifyThreadPrint(new String[]{"a", "b", "c", "d", "e", "f"}, lock));
        t1.start();
        t2.start();

    }

    public void run() {
        synchronized (lock) {
            for (String caa : arr) {
                System.out.print(caa);
                lock.notify();
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            lock.notify();
        }
    }
}
